package com.pig4cloud.pigx.system.websocket;

import com.pig4cloud.pigx.common.customize.redis.constants.CommonRedisConstants;
import com.pig4cloud.pigx.system.constant.RedisHashKeyConstant;
import com.pig4cloud.pigx.system.service.SystemSymbolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
@Order(0)
@Component
public class FetchManager implements ApplicationRunner {

    private static Map<String, String> parsedata_okx = new HashMap<>();

    private long lasttime_okx;

    @Autowired
    private SystemSymbolService symbolService;

    public static final int expire_millisecond = 10000;

    @Autowired
    private RedisTemplate redis;

    public static Set<String> robotConfigList;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("fetch data init...");
        init();
    }

    private void init() {
        initSymbol();
        startFetch();
        flush_redis_data();
    }

    private void startFetch() {
        com.pig4cloud.pigx.system.websocket.okey.WsManager.getInstance().init(this);
    }

    private void reFetch() {
        com.pig4cloud.pigx.system.websocket.okey.WsManager.getInstance().reConnection();
    }

    private void endFetch() {
        com.pig4cloud.pigx.system.websocket.okey.WsManager.getInstance().onClose();
    }

    private void initSymbol() {
        FetchManager.robotConfigList = redis.opsForHash().keys(RedisHashKeyConstant.SYMBOL_LIST);
    }

    public void setdata(String key, String v) {
        if (key.contains(CommonRedisConstants.OKX)) {
            parsedata_okx.put(key, v);
            lasttime_okx = System.currentTimeMillis();
        }
    }

    /**
     * 刷新redis data
     */
    private void flush_redis_data() {
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleAtFixedRate(() -> {
            try {
                FetchManager.robotConfigList = redis.opsForHash().keys(RedisHashKeyConstant.SYMBOL_LIST);
                if (redis != null) {
                    if (parsedata_okx.size() > 0 && System.currentTimeMillis() - lasttime_okx < expire_millisecond) {
                        parsedata_okx.forEach((key, value) -> {
//                            String k = key.replace(CommonRedisConstants.OKX, "");
                            String[] split = value.split("_");
                            if (split.length > 0 && split[0] != null) {
                                redis.opsForValue().set(key, split[0]);
                            }
                        });
                    }
                }
            } catch (Exception ex) {
                log.error("flush_redis_data error ", ex.getMessage());
            }
        }, 200, 200, TimeUnit.MILLISECONDS);
    }


}
